package com.gr.jron.model;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * Created by Jronrun.
 * User: jron
 * Date: 2007-7-21
 * Time: 10:39:12
 * It's jron's dream to cqiu
 */
@SuppressWarnings({"JpaModelErrorInspection"})
@Entity
@Table(name = "JCATEGORIZEDPRODUCT")
public class CategorizedProduct extends BaseObject implements Comparable {

    @EmbeddedId
    private Id id;

    private String username;
    private Product categorizedProduct;
    private Category category;
    private Date dateAdded;

    public Id getId() {
        return id;
    }

    public CategorizedProduct() {
    }

    public CategorizedProduct(String username, Product product, Category category) {
        this.username = username;
        this.categorizedProduct = product;
        this.category = category;

        this.id = new Id(category.getId(), product.getId());

        category.getCategorizedProducts().add(this);
        product.getCategorizedProducts().add(this);
    }

    @Column(name = "ADDED_BY_USER", nullable = false, length = 32)
    public String getUsername() {
        return username;
    }

    @Column(name = "ADDED_ON", updatable = false)
    public Date getDateAdded() {
        return dateAdded;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    public Product getProduct() {
        return categorizedProduct;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    public Category getCategory() {
        return category;
    }

    public void setDateAdded(Date dateAdded) {
        this.dateAdded = dateAdded;
    }

    @Embeddable
    public static class Id extends BaseObject implements Serializable {
        @Column(name = "CATEGORY_ID")
        private Long categoryId;
        @Column(name = "PRODUCT_ID")
        private Long productId;

        public Id() {
        }

        public Id(Long productId, Long categoryId) {
            this.productId = productId;
            this.categoryId = categoryId;
        }

        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Id id = (Id) o;
            if (categoryId != null ? !categoryId.equals(id.categoryId) : id.categoryId != null) return false;
            if (productId != null ? !productId.equals(id.productId) : id.productId != null) return false;
            return true;
        }

        public int hashCode() {
            int result;
            result = (categoryId != null ? categoryId.hashCode() : 0);
            result = 31 * result + (productId != null ? productId.hashCode() : 0);
            return result;
        }

        public String toString() {
            return "Id{ categoryId=" + categoryId + ", productId=" + productId + '}';
        }
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        CategorizedProduct that = (CategorizedProduct) o;

        if (dateAdded != null ? !dateAdded.equals(that.dateAdded) : that.dateAdded != null) return false;
        if (id != null ? !id.equals(that.id) : that.id != null) return false;
        if (username != null ? !username.equals(that.username) : that.username != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (id != null ? id.hashCode() : 0);
        result = 31 * result + (username != null ? username.hashCode() : 0);
        result = 31 * result + (dateAdded != null ? dateAdded.hashCode() : 0);
        return result;
    }

    public String toString() {
        return "CategorizedProduct{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", dateAdded=" + dateAdded +
                '}';
    }

    public int compareTo(Object o) {
        // CategorizedItems are sorted by date
        if (o instanceof CategorizedProduct)
            return getDateAdded().compareTo(((CategorizedProduct) o).getDateAdded());
        return 0;
    }
}
